<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="100px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="图片信息" prop="description">
        <el-input v-model="dataForm.description" placeholder="请输入图片信息"/>
      </el-form-item>

      <el-form-item label="类别" prop="categoryId">
        <el-select v-model="dataForm.categoryId" placeholder="请选择图片分类">
          <el-option v-for="item in allCategory" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="图片" prop="image">
        <upload-image v-model="dataForm.image"/>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getPicture, putPicture, postPicture } from '@/api/picture'
import { getAllCategory } from '@/api/category'
import UploadImage from '@/components/UploadImage'
const defaultDataForm = { id: 0, description: '', categoryId: '', image: undefined, createTime: '', updateTime: '' }

export default {
  components: { UploadImage },
  data() {
    return {
      visible: false,
      dataForm: defaultDataForm,
      category: Object,
      allCategory: [],
      dataRule: {
        description: [
          { required: true, message: '图片信息不能为空', trigger: 'blur' },
          { max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ],
        categoryId: [
          { required: true, message: '分类不能为空', trigger: 'blur' }
        ]
      }

    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getPicture(id).then(response => {
            this.dataForm = response.data
            this.category = response.data.category
          })
        } else {
          this.dataForm = defaultDataForm
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (!this.dataForm.image) {
            this.dataForm.image = undefined
          }
          if (this.dataForm.id) {
            putPicture(this.dataForm).then(response => {
              this.$message.success('修改用户成功')
              this.visible = false
              this.$emit('refreshDataList')
            })
          } else {
            postPicture(this.dataForm).then(response => {
              this.$message.success('添加用户成功')
              this.visible = false
              this.$emit('refreshDataList')
            })
          }
        }
      })
    },
    //查询全部分类
    getAllCategory(){
      getAllCategory().then(res=>{
        this.allCategory = res.data || []
      }).catch(err=>{
        console.log(err)
      })
    }
  },
  mounted(){
    this.getAllCategory()
  },
}
</script>
